//
//  UIView+BCEmpty.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/3.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

static NSInteger UIViewEmptyDataViewTag = 100102;

#import "UIView+BCEmpty.h"

@implementation UIView (BCEmpty)

- (void)bc_showEmptyDataText:(nullable NSString *)text
                      detail:(nullable NSString *)detail {
    [self bc_showEmptyDataInsets:UIEdgeInsetsZero backgroundColor:[UIColor generalWhiteBackgroundColor] image:nil text:text detail:detail];
}

- (void)bc_showEmptyDataInsets:(UIEdgeInsets)insets
                          text:(nullable NSString *)text
                        detail:(nullable NSString *)detail {
    [self bc_showEmptyDataInsets:insets backgroundColor:[UIColor generalWhiteBackgroundColor] image:nil text:text detail:detail];
}
- (void)bc_showEmptyDataInsets:(UIEdgeInsets)insets
                         image:(nullable UIImage *)image
                          text:(nullable NSString *)text
                        detail:(nullable NSString *)detail {
    [self bc_showEmptyDataInsets:insets backgroundColor:[UIColor generalWhiteBackgroundColor] image:image text:text detail:detail];
}

/**
 * !@biref 显示无数据的空白视图
 * @param insets 相对内边距
 * @param backgroundColor 背景色
 * @param image 显示image
 * @param text title
 * @param detail detail
 */
- (void)bc_showEmptyDataInsets:(UIEdgeInsets)insets
               backgroundColor:(nullable UIColor *)backgroundColor
                         image:(nullable UIImage  *)image
                          text:(nullable NSString *)text
                        detail:(nullable NSString *)detail {
    QMUIEmptyView *emptyView = [self viewWithTag:UIViewEmptyDataViewTag];
    if (!emptyView) {
        emptyView = [QMUIEmptyView new];
        emptyView.tag = UIViewEmptyDataViewTag;
        [self addSubview:emptyView];
        [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([self isKindOfClass:UIScrollView.class]) {
                make.top.mas_equalTo(self).offset(insets.top);
                make.left.mas_equalTo(self).offset(insets.left);
                make.width.mas_equalTo(self).offset(-insets.left-insets.right);
                make.height.mas_equalTo(self).offset(-insets.top-insets.bottom);
            }else {
                make.edges.mas_equalTo(self).insets(insets);
            }
        }];
        emptyView.verticalOffset = -30.0;
    }
    emptyView.backgroundColor = backgroundColor;
    //
    [emptyView setLoadingViewHidden:true];
    [emptyView setImage:image];
    [emptyView setTextLabelText:text];
    [emptyView setDetailTextLabelText:detail];
    
}

//
- (void)bc_hidderEmptyData {
    QMUIEmptyView *emptyView = [self viewWithTag:UIViewEmptyDataViewTag];
    if (emptyView) {
        [emptyView removeFromSuperview];
    }
}

@end
